package com.wanmait.recruit.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wanmait.recruit.pojo.*;
import com.wanmait.recruit.service.*;
import com.wanmait.recruit.util.PageContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/web/company/manage/resume")
public class CompanyResumeController {

    @Resource
    CompanyResumeService companyResumeService;

    @Resource
    ResumeService resumeService;

    @Resource
    CompanyHrService companyHrService;

    @Resource
    EducationExpService educationExpService;

    @Resource
    PositionDesiredService positionDesiredService;

    @Resource
    SkillLabelInfoService skillLabelInfoService;

    @Resource
    WorkExpService workExpService;

    @Resource
    ProjectExpService projectExpService;

    @Resource
    PersonInfoService personInfoService;

    @RequestMapping("/info")
    public ModelAndView info(Integer id){
        ModelAndView mv = new ModelAndView();
        mv.setViewName("/web/company/manage/resume/info");
        CompanyHrReceiveResume companyHrReceiveResume = companyResumeService.findById(id);
        Resume resume = resumeService.findById(companyHrReceiveResume.getResumeId());
        EducationExp educationExp = educationExpService.findByResume(resume);
        PositionDesired positionDesired = positionDesiredService.findByResume(resume);
        List<SkillLabel> skillLabels = skillLabelInfoService.findByResume(resume);
        WorkExp workExp = workExpService.findByResume(resume);

        ProjectExp projectExp = projectExpService.findByResume(resume);
        mv.addObject(companyHrReceiveResume);
        mv.addObject(resume);
        mv.addObject(educationExp);
        mv.addObject(positionDesired);
        mv.addObject(skillLabels);
        mv.addObject(workExp);
        mv.addObject(projectExp);

        return mv;
    }
    //删除简历
    @RequestMapping("/delete")
    public void delete(Integer id){
        CompanyHrReceiveResume companyHrReceiveResume = new CompanyHrReceiveResume();
        companyHrReceiveResume.setId(id);
        companyResumeService.delete(companyHrReceiveResume);
    }
    //接受简历
    @RequestMapping("/accept")
    public ModelAndView accept(Integer id,HttpSession session){
        ModelAndView mv = new ModelAndView();
        mv.setViewName("redirect:/web/company/manage/resume/list");
        CompanyHrReceiveResume companyHrReceiveResume = new CompanyHrReceiveResume();
        companyHrReceiveResume.setId(id);
        companyHrReceiveResume = companyResumeService.findById(id);
        Person person = (Person)session.getAttribute("person");
        CompanyHr companyHr = companyHrService.findByPerson(person);
        PersonInfo personInfo = new PersonInfo();
        personInfo.setResourcePersonId(person.getId());
        personInfo.setContent("您投往"+companyHr.getCompany().getCompanyName()+"的岗位名为"+companyHrReceiveResume.getCompanyPosition().getName()+"的简历已被接受，请等待公司电话联系");
        personInfo.setPersonId(companyHrReceiveResume.getResume().getPerson().getId());
        //向用户发送接受消息
        personInfoService.insert(personInfo);

        return mv;
    }
    //拒绝简历并删除
    @RequestMapping("/refuse")
    public ModelAndView refuse(Integer id,HttpSession session){
        ModelAndView mv = new ModelAndView();
        mv.setViewName("redirect:/web/company/manage/resume/list");
        CompanyHrReceiveResume companyHrReceiveResume = new CompanyHrReceiveResume();
        companyHrReceiveResume.setId(id);
        companyHrReceiveResume = companyResumeService.findById(id);
        Person person = (Person)session.getAttribute("person");
        CompanyHr companyHr = companyHrService.findByPerson(person);
        PersonInfo personInfo = new PersonInfo();
        personInfo.setResourcePersonId(person.getId());
        personInfo.setContent("您投往"+companyHr.getCompany().getCompanyName()+"的岗位名为"+companyHrReceiveResume.getCompanyPosition().getName()+"的简历已被拒绝");
        personInfo.setPersonId(companyHrReceiveResume.getResume().getPerson().getId());
        //向用户发送拒绝消息
        personInfoService.insert(personInfo);
        //删除简历
        companyResumeService.delete(companyHrReceiveResume);

        return mv;
    }

    @RequestMapping("/listData")
    public ModelAndView resumeListData(HttpSession session){
        Person person = (Person)session.getAttribute("person");
        ModelAndView mv = new ModelAndView();
        mv.setViewName("/web/company/manage/resume/listData");
        CompanyHr companyHr = companyHrService.findByPerson(person);

        PageInfo<CompanyHrReceiveResume> pageInfo = companyResumeService.findByHr(PageContext.getPageNum(),1,companyHr);

        mv.addObject(pageInfo);
        return mv;
    }


    @RequestMapping("/list")
    public ModelAndView resumeList(HttpSession session){
        Person person = (Person)session.getAttribute("person");
        ModelAndView mv = new ModelAndView();
        mv.setViewName("/web/company/manage/resume/list");
        CompanyHr companyHr = companyHrService.findByPerson(person);

        PageInfo<CompanyHrReceiveResume> pageInfo = companyResumeService.findByHr(PageContext.getPageNum(),1,companyHr);

        mv.addObject(pageInfo);
        return mv;
    }
}
